package kr.couponplay.android.activity;

import java.util.ArrayList;

import kr.couponplay.android.AppContext;
import kr.couponplay.android.Category;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings.Secure;
import android.util.Log;
import android.view.Window;

@SuppressLint("HandlerLeak") 
public class IntroActivity extends BaseActivity {
	SharedPreferences preferences;
	long startTime = 0L;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main_intro);
        
        //메인페이지 꾸미기
        initialize();
    }
    
    public void initialize() {
    	startTime = System.currentTimeMillis();
    	Thread thread = new Thread(new Runnable() {
            public void run() {
            	try {
            		loadMyPhoneNumber();
            		loadServiceInfo();
            		loadCategoryData();
            	} catch (Exception e) {
            		Log.e(AppContext.TAG, e.getMessage(), e);
            		throw new IllegalArgumentException();
            	}
        		long dulation = System.currentTimeMillis() - startTime;
        		handler.sendEmptyMessageDelayed(0, (dulation < 1000)? 1000 - dulation : 0);
            }

			private void loadServiceInfo() {
				AppContext.setServiceInfo(dao.getServiceInfo());
			}

			private void loadMyPhoneNumber() {
				String androidId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
//				preferences = mContext.getSharedPreferences(AppContext.PREFERENCE_NAME, Activity.MODE_PRIVATE);
//		    	String sid = preferences.getString("SID", null);
		    	String sid = androidId;
				if (sid == null) {
		    		sid = String.valueOf(System.currentTimeMillis());
//		    		Editor editor = preferences.edit();
//		            editor.putString("SID", sid);
		    	}
		    	AppContext.sid = sid;
			}
        });
        thread.start();
    }
    
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
			finish();
	    	Intent intent = new Intent(mContext, MainActivity.class);
			startActivity(intent);
        }
    };
    
	private void loadCategoryData() {
		ArrayList<Category> groups = new ArrayList<Category>();
		ArrayList<ArrayList<ArrayList<Category>>> childs = new ArrayList<ArrayList<ArrayList<Category>>>();
		
		Category root = AppContext.getCategory();
		
		if (root == null) {
			return;
		}
		
		for (int i=0; i < root.getChildren().size(); i++) {
			Category group = root.getChildren().get(i);
			groups.add(group);
			childs.add(new ArrayList<ArrayList<Category>>());
			for (int j=0; j < group.getChildren().size(); j++) {
				Category child = group.getChildren().get(j);
				childs.get(i).add(new ArrayList<Category>());
				childs.get(i).get(j).add(child);
			}
		}
		AppContext.categoryGroups = groups;
		AppContext.categoryChildren = childs;
	}    
}
